package com.kyle.silverbullet.client.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;

import java.time.OffsetDateTime;

/**
 * @author kyle
 * @date 2020-07-23
 */
public class BaseEntity {
    /**
     * id无业务意义主键
     */
    @TableId(value = "id", type = IdType.ASSIGN_UUID)
    protected String id;
    /**
     * 创建日期
     * <p>
     * 注意， LocalDateTime对应数据库的类型必须为timestamp 而不是timestampz
     * 不然的话，就会报
     * org.postgresql.util.PSQLException: Cannot convert the column of type TIMESTAMPTZ to requested type timestamp.]
     * <p>
     * 具体处理代码请查看
     * {@link org.postgresql.jdbc.PgResultSet#getLocalDateTime(int)}
     * <p>
     * 注意，这个处理函数里面从 ResultSet 中获取了对应列的 OID
     */
    @TableField(value = "gmt_create", fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    protected OffsetDateTime gmtCreate;

    /**
     * 更新日期
     * <p>
     * JsonFormat会使得底层重新创建一个新的序列化器来进行序列化任务
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @TableField(value = "gmt_modified", update = "now()", fill = FieldFill.UPDATE)
    protected OffsetDateTime gmtModified;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public OffsetDateTime getGmtCreate() {
        return gmtCreate;
    }

    public void setGmtCreate(OffsetDateTime gmtCreate) {
        this.gmtCreate = gmtCreate;
    }

    public OffsetDateTime getGmtModified() {
        return gmtModified;
    }

    public void setGmtModified(OffsetDateTime gmtModified) {
        this.gmtModified = gmtModified;
    }
}
